// TestControl.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "TestControl.h"

#define MSGBOX(msg) MessageBox(0, msg, msg, MB_OK)

class CMainFrameWnd : public CWindowWnd , public INotifyUI, IListCallbackUI
{
private:
	CPaintManagerUI m_pm;

public:
	void Notify(TNotifyUI& msg)
	{
		if (msg.sType == _T("windowinit"))
		{
			//MSGBOX(_T("windowinit"));
		}
		else if (msg.sType == _T("itemclick"))
		{
			CListExpandElementUI* pItem = (CListExpandElementUI*)msg.pSender;
			CLabelUI* pLabel = new CLabelUI;
			pLabel->SetText(_T("itemexpand"));
			pItem->Expand();
		}
	}

	LPCTSTR GetWindowClassName() const
	{
		return _T("MainFrameWnd");
	}

	UINT GetClassStyle() const
	{
		return UI_CLASSSTYLE_FRAME;
	}

	LRESULT OnCreate()
	{
		m_pm.Init(m_hWnd);
		CDialogBuilder builder;
		CControlUI* pRoot = builder.Create(_T("Dlg.xml"), UINT(0), NULL, &m_pm);
		m_pm.AttachDialog(pRoot);
		m_pm.AddNotifier(this);

		CListUI* pList = (CListUI*)m_pm.FindControl(_T("Listview"));
		pList->SetTextCallback(this);
		return 0;
	}

	LPCTSTR GetItemText(CControlUI* pList, int iItem, int iSubItem)
	{
		static TCHAR buf[MAX_PATH];
		wsprintf(buf, _T("Item:%d SubItem:%d this:%p"), iItem, iSubItem, pList);
		return buf;
	}

	void OnFinalMessage(HWND hWnd)
	{
		delete this;
	}

	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		LRESULT lrs=0;
		switch (uMsg)
		{	
		case WM_CREATE:
			return OnCreate();
		case WM_CLOSE:
			PostQuitMessage(WM_QUIT);
			return 0;
		default:
			if (!m_pm.MessageHandler(uMsg, wParam, lParam, lrs))
			{
				return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
			}
		}
		return lrs;
	}
};



int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

	CoInitialize(NULL);

	CMainFrameWnd* pMainFrame = new CMainFrameWnd;
	pMainFrame->Create(NULL, _T("Dlg"), UI_WNDSTYLE_FRAME,0);
	pMainFrame->CenterWindow();
	pMainFrame->ShowWindow();
	
	CPaintManagerUI::MessageLoop();

	CoUninitialize();
	return 0;
}